<?php
/**
 * @copyright Copyright (c) 2021 勾股工作室
 * @license https://opensource.org/licenses/Apache-2.0
 * @link https://www.gougucms.com
 */

// 应用公共文件,内置主要的数据处理方法
use think\facade\Config;
use think\facade\Request;
use think\facade\Cache;
use think\facade\Db;

//获取后台模块当前登录用户的信息
function get_login_admin($key = "")
{
    $session_admin = get_config('app.session_admin');
    if (\think\facade\Session::has($session_admin)) {
        $gougu_admin = \think\facade\Session::get($session_admin);
        if (!empty($key)) {
            if (isset($gougu_admin[$key])) {
                return $gougu_admin[$key];
            } else {
                return '';
            }
        } else {
            return $gougu_admin;
        }
    } else {
        return '';
    }
}

//递归排序，用于分类选择
function set_recursion($result, $pid = 0, $level = -1)
{
    /*记录排序后的类别数组*/
    static $list = array();
    static $space = ['', '├─', '§§├─', '§§§§├─', '§§§§§§├─'];
    $level++;
    foreach ($result as $k => $v) {
        if ($v['pid'] == $pid) {
            if ($pid != 0) {
                $v['name'] = $space[$level] . $v['name'];
                $v['level'] = $level;
            }
            /*将该类别的数据放入list中*/
            $list[] = $v;
            set_recursion($result, $v['id'], $level);
        }
    }
    return $list;
}

/**
 * 根据id递归返回子数据
 * @param  $data 数据
 * @param  $pid 父节点id
 */
function get_data_node($data = [], $pid = 0)
{
    $dep = [];
    foreach ($data as $k => $v) {
        if ($v['pid'] == $pid) {
            $node = get_data_node($data, $v['id']);
            array_push($dep, $v);
            if (!empty($node)) {
                $dep = array_merge($dep, $node);
            }
        }
    }
    return array_values($dep);
}

/**
 * 管理员操作日志
 * @param string $type 操作类型 login add edit view delete
 * @param int $param_id 操作类型
 * @param array $param 提交的参数
 * @param subject $param 操作主题
 */
function add_log($type, $param_id = '', $param = [], $subject = '')
{
    $action = '未知操作';
    $type_action = get_config('log.admin_action');
    if ($type_action[$type]) {
        $action = $type_action[$type];
    }
    if ($type == 'login') {
        $login_admin = Db::name('Admin')->where(array('id' => $param_id))->find();
    } else {
        $session_admin = get_config('app.session_admin');
        $login_admin = \think\facade\Session::get($session_admin);
    }
    $data = [];
    $data['title'] = '';
    $data['uid'] = $login_admin['id'];
    $data['nickname'] = $login_admin['nickname'];
    $data['type'] = $type;
    $data['action'] = $action;
    $data['param_id'] = $param_id;
    $data['param'] = json_encode($param);
    $data['module'] = strtolower(app('http')->getName());
    $data['controller'] = uncamelize(app('request')->controller());
    $data['function'] = strtolower(app('request')->action());
    $parameter = $data['module'] . '/' . $data['controller'] . '/' . $data['function'];
    $rule_menu = Db::name('AdminRule')->where(array('src' => $parameter))->find();
    if ($rule_menu) {
        $data['title'] = $rule_menu['title'];
        $data['subject'] = $rule_menu['name'];
    } else {
        if (empty($subject)) {
            $data['subject'] = '系统';
        } else {
            $data['subject'] = $subject;
        }
    }
    $content = $login_admin['nickname'] . '在' . date('Y-m-d H:i:s') . $data['action'] . '了' . $data['subject'];
    $data['content'] = $content;
    $data['ip'] = app('request')->ip();
    $data['create_time'] = time();
    Db::name('AdminLog')->strict(false)->field(true)->insert($data);
}

//读取部门列表
function get_unit_list($area_id)
{
    return Db::table('m_unit')->where('area_id', $area_id)->whereNull('delete_time')->select()->toArray();
}

//读取目录
function get_directory_list($area_id)
{
    return Db::table('m_matter_directory')->where('area_id',$area_id)->whereNull('delete_time')->field('id,pid,name')->select()->toArray();
}

//读取情形列表
function get_case_list($matter_id = 0)
{
    $where = array();
    if ($matter_id > 0) {
        $where[] = ['matter_id', '=', $matter_id];
    }
    return Db::table('m_matter_case')->where($where)->whereNull('delete_time')->select()->toArray();
}

//获取某情形的子情形id.$is_self时候包含自己
function get_matter_case_son($did = 0, $is_self = 1)
{
    $department = get_case_list();
    $department_list = get_data_node($department, $did);
    $department_array = array_column($department_list, 'id');
    if ($is_self == 1) {
        //包括自己在内
        $department_array[] = $did;
    }
    return $department_array;
}

//读取主题列表
function get_theme_list($service_type)
{
    return Db::table('m_theme')->where('service_type', $service_type)->whereNull('delete_time')->select()->toArray();
}

//
function get_area($area_id)
{
    $area_list1 = [];
    get_parent_dep($area_list1,$area_id);
    $area_list1 = Db::table('m_area')->where('id','in',$area_list1)->field('id,pid,name')->select()->toArray();
    $area_list2 = get_data_node(get_area_list(5), $area_id);
    return array_merge($area_list1, $area_list2);
}